﻿using LD.BC;
using LD.Host;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LD.Tool
{
    public partial class ToolIOGroupMonitorByUserFrm : FrmToolBaseSkip//FrmToolBase
    {
        public ToolIOGroupMonitorByUserFrm()
        {
            InitializeComponent();
            this.Shown += ToolMoveFrm_Shown;
            this.btn_ok.Click += Btn_ok_Click;
        }

        private void Btn_ok_Click(object sender, EventArgs e)
        {
            List<IoMonitor> ioMonitors = new List<IoMonitor>();
            foreach (DataGridViewRow item in this.dgv_data.Rows)
            {
                if (item.Cells["IO类型"].Value != null &&
                    item.Cells["IO名称"].Value != null &&
                    item.Cells["IO信号"].Value != null &&
                    int.TryParse(this.txt_timeOut.Text, out var timeDate))
                {
                    int ioType = Convert.ToInt16(item.Cells["IO类型"].Value.ToString());
                    int ioNo = Convert.ToInt16(item.Cells["IO名称"].Value.ToString());
                    string ioName = string.Empty; //item.Cells["IO名称"].Value.ToString();
                    int level = Convert.ToInt16(item.Cells["IO信号"].Value.ToString());

                    ioMonitors.Add(new IoMonitor()
                    {
                        IoName = ioName,
                        IoType = ioType,
                        Low_Hig = level,
                        No = ioNo,
                    });
                    var param = new ToolIOGroupMonitorByUserParam()
                    {
                        IoMonitors = ioMonitors,
                        TimeDate = timeDate,
                        IsTimeOutMonitor = this.sw_timeOut.Checked
                    };

                    this.TB_Tool.Param = JsonConvert.SerializeObject(param);
                }
            }
        }

        private void ToolMoveFrm_Shown(object sender, EventArgs e)
        {
            Init();
        }
        private DataTable GetLevel()
        {
            string[] str = Enum.GetNames(typeof(LevelType)).ToArray();
            DataTable dt = new DataTable();
            dt.Columns.Add("id");
            dt.Columns.Add("name");
            for (int i = 0; i < str.Length; i++)
            {
                dt.Rows.Add(i, str[i]);
            }
            return dt;
        }
        private DataTable GetIoType()
        {
            string[] str = Enum.GetNames(typeof(IoType)).ToArray();
            DataTable dt = new DataTable();
            dt.Columns.Add("id");
            dt.Columns.Add("name");
            for (int i = 0; i < str.Length; i++)
            {
                dt.Rows.Add(i, str[i]);
            }
            return dt;
        }
        private void Init()
        {
            DataTable level = GetLevel();
            this.IO信号.DataSource = level;
            this.IO信号.ValueMember = "id";
            this.IO信号.DisplayMember = "name";

            DataTable ioType = GetIoType();
            this.IO类型.DataSource = ioType;
            this.IO类型.ValueMember = "id";
            this.IO类型.DisplayMember = "name";

            if (this.TB_Tool.Param == null)
                return;
            var param = JsonConvert.DeserializeObject<ToolIOGroupMonitorByUserParam>(this.TB_Tool.Param);
            if (param != null)
            {
                this.sw_timeOut.Checked = param.IsTimeOutMonitor;
                this.txt_timeOut.Text = param.TimeDate.ToString();
                foreach (var item in param.IoMonitors)
                {
                    int index = this.dgv_data.Rows.Add();
                    //this.dgv_data["IO编号", index].Value = item.No;
                    DataGridViewComboBoxCell cobCellName1 = this.dgv_data["IO类型", index] as DataGridViewComboBoxCell;
                    cobCellName1.Value = item.IoType + "";

                    DataGridViewComboBoxCell cobCellName2 = this.dgv_data["IO信号", index] as DataGridViewComboBoxCell;
                    cobCellName2.Value = item.Low_Hig + "";

                    var io = BcFactory.Instance.Config.IoList.Where(p => p.IoType == item.IoType).ToList();
                    DataGridViewComboBoxCell cobCellName3 = this.dgv_data["IO名称", index] as DataGridViewComboBoxCell;
                    cobCellName3.DataSource = io;
                    cobCellName3.DisplayMember = "Name";
                    cobCellName3.ValueMember = "No";
                    cobCellName3.Value = item.No;
                }
            }
        }
        private void tsb_new_Click(object sender, EventArgs e)
        {
            this.dgv_data.Rows.Add();
        }

        private void dgv_data_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0) return;
            if (e.ColumnIndex == 0 && this.dgv_data[e.ColumnIndex, 0].Value != null)
            {
                if (int.TryParse(this.dgv_data[e.ColumnIndex, 0].Value.ToString(), out var iotype))
                //&& int.TryParse(this.dgv_data[e.ColumnIndex, 1].Value.ToString(), out var iono))
                {
                    var io = BcFactory.Instance.Config.IoList.Where(p => p.IoType == iotype).ToList();
                    if (io.Any())
                    {
                        DataGridViewComboBoxCell cobCellName1 = this.dgv_data["IO名称", e.RowIndex] as DataGridViewComboBoxCell;
                        cobCellName1.DataSource = io;
                        cobCellName1.DisplayMember = "Name";
                        cobCellName1.ValueMember = "No";
                    }
                    //this.dgv_data[e.ColumnIndex, 1].Value = io.Name;
                }
            }
        }

        private void tsb_sub_Click(object sender, EventArgs e)
        {
            this.dgv_data.SelectedRows.Clear();
        }
    }
}
